package com.bangcommunity.bbframe.dynconfig;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.leader.LeaderLatch;
import org.apache.curator.framework.recipes.leader.LeaderLatchListener;

import com.bangcommunity.bbframe.client.zookeeper.ZKClient;
import com.bangcommunity.bbframe.common.utils.id.DangDangIdFactory;

/**
 * Created by tanghc on 17/8/26.
 */
public class LeaderLatchTest1 {
    private static final String PATH = "/LeaderLatchTest/leader";
    private static final String uid = "1111-" + DangDangIdFactory.getInstance().getIdGenerator("uid").generateId();

    public static void main(String[] args) throws Exception {
        CuratorFramework client = ZKClient.getClient();
        final LeaderLatch leaderLatch = new LeaderLatch(client, PATH, uid);
        leaderLatch.addListener(new LeaderLatchListener() {
            @Override
            public void isLeader() {
                System.out.println(leaderLatch.getId() + ":I am leader. I am doing jobs!");
            }

            @Override
            public void notLeader() {
                System.out.println(leaderLatch.getId() + ":I am not leader. I will do nothing!");
            }
        });
        leaderLatch.start();

        System.in.read();

    }
}
